package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.data.rxbus.ChangeStoreMessage;
import com.hongyuan.sellfitness.ui.home.activity.StoreListActivity;
import com.hongyuan.sellfitness.ui.home.beans.StoreInfoBean;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class ReservationViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public ObservableField<StoreInfoBean> entity = new ObservableField<>();

    public ReservationViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //门店列表
    public BindingCommand goStoreList = new BindingCommand(() -> startActivity(StoreListActivity.class));

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(ChangeStoreMessage.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(progressLoadBean -> {
                    curPage = FIRST_PAGE;

                    //更新下数据
                    setParams("os_id",progressLoadBean.getOs_id());

                    requestData(Constants.GET_OFFLINE_STORE_INFO);
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);
        if(code == Constants.GET_OFFLINE_STORE_INFO){
            StoreInfoBean infoBean = (StoreInfoBean)dataBean;

            entity.set(infoBean);
        }
    }
}
